import { area, pokerSTatus } from "../common/enumConfig";
import gameModel, { Poker } from "./gameModel";
import UIPoker from "../common/UIPoker";
import View from "../common/view";
import gameEvent from "../common/gameEvent";
import UIUtil from "../common/UIUtil";

const {ccclass, property} = cc._decorator;

@ccclass
export default class gameView extends View {
    @property(cc.Prefab)
    uiPokerPre:cc.Prefab = null
    @property(cc.Node)
    initPokersArea:cc.Node = null
    @property(cc.Node)
    closeSendArea:cc.Node = null
    @property(cc.Node)
    openSendArea:cc.Node = null
    @property(cc.Node)
    PlayGroupRoot:cc.Node = null
    @property(cc.Button)
    restartBtn:cc.Button = null
    @property([cc.Node])
    receivePokerAreaList:cc.Node[] = []

    private gameModel:gameModel = null
    private pokerNodePool:cc.NodePool = null
    private pokerArr = []

    onLoad(){
        this.restartBtn.node.on('touchstart',()=>{
            this.emit(gameEvent.RESTART)
        },this)
    }

    public bindModel(gameModel:gameModel){
        this.gameModel = gameModel
    }

    /*******************************************************************
     * private Api
     * ***************************************************************/
    //创建扑克
     private createPoker(poker:Poker):UIPoker{
         let uiPokerNode:cc.Node = null
         if(this.pokerNodePool.size()>0){
             uiPokerNode = this.pokerNodePool.get()
         }else{
            uiPokerNode = cc.instantiate(this.uiPokerPre)
         }
        let uiPoker:UIPoker = uiPokerNode.getComponent(UIPoker)
        uiPoker.init(poker,this)
        return uiPoker

    }

        // 初始化扑克
     private initPokers(pokers:Poker[]){
         if(!this.pokerNodePool) this.pokerNodePool = new cc.NodePool()
        // 创建所有扑克UI
        pokers.forEach((poker,index)=>{
            let uiPoker = this.createPoker(poker)
            uiPoker.node.x = 0.2*index
            uiPoker.node.y = 0.2*index
            this.initPokersArea.addChild(uiPoker.node)
        })
    }

        // 获取全部扑克牌
    findNodeOfPoker(parent:cc.Node){
        let count = parent.childrenCount
        if(count){
            if(parent.name == "poker"){
                return parent
            }else{
                for(let i=0;i<count;i++){
                   let node = this.findNodeOfPoker(parent.children[i])
                   if(node){
                       this.pokerArr.push(node)
                   }
            }
            } 
        }
        else{
            return null
        }
    }

        // 开始游戏
    private onPlay(){
        for(let i = this.initPokersArea.children.length-1;i>=0;--i){
            let child = this.initPokersArea.children[i]
            this.initPokersArea.removeChild(child,false)
            this.closeSendArea.addChild(child)
        }

        this.gameModel.closeAreaPokers.forEach((poker,index)=>{poker.view.node.zIndex = index})
    }



    /*******************************************************************
     * Interface for UIPoker
     ******************************************************************/
    // 点击扑克时
    public onClickUIPoker(UIPoker:UIPoker){
        /* 
        *   1.是否在玩牌区
        *   2.是否是翻开的
        */
        let poker = UIPoker.poker
        if(this.gameModel.isLocationArea(poker,area.PLATY_AREA)){
            if(UIPoker.isOpen()){
               this.emit(gameEvent.POKER_MOVE_PLAYAREA_TO_RECIEVEAREA,poker)
            }
        }
        /*
         * 1.是否在收牌区
            2.是否最上面的牌
         **/
        else if(this.gameModel.isLocationArea(poker,area.CLOSE_AREA)){
            if(this.gameModel.isCloseAreaIndexTop(poker)){
                console.log('收牌区')
            }
        }
    }

    /*******************************************************************
     * event handle Api
     ******************************************************************/
    // 初始化事件响应
    public onEventInit(pokers){
        this.initPokers(pokers)
    }

    //游戏开始事件响应
    public onEventPlay(){
        this.onPlay()
    }

    // 移动扑克动画事件响应
    public onEventInitGroupCard(cardGroupIndex:number,cardIndex:number,poker:Poker,index:number){
        let node = poker.view.node
        UIUtil.move(node,this.PlayGroupRoot)
        node.zIndex = index
        let px = cardGroupIndex*100
        
        if(poker.status === pokerSTatus.OPEN){
            cc.tween(node).delay(0.05*index).to(0.5,{position:cc.v3(px,-30*cardIndex,0)}).to(0.3,{scaleX:0}).call(()=>{
                poker.view.refresh()
            }).to(0.3,{scaleX:0.8}).start()
        }else{
            cc.tween(node).delay(0.05*index).to(0.5,{position:cc.v3(px,-30*cardIndex,0)}).start()
        }

    }

    // 扑克牌移动到收牌区
    public onEventMovePokerToReceiveArea(poker:Poker,index:number){
        let receivePokerArea = this.receivePokerAreaList[index]
        let node = poker.view.node
        UIUtil.move(node,receivePokerArea)
        node.zIndex = poker.point
        cc.tween(node).to(0.5,{position:cc.v3(0,0,0)}).start()
    }

    //扑克牌翻牌
    public onEventRestatusPoker(poker:Poker){
        if(poker.status === pokerSTatus.OPEN){
            cc.tween(poker.view.node).to(0.3,{scaleX:0}).call(()=>{poker.view.refresh()}).to(0.3,{scaleX:0.8}).start()
        }
    }

    /*******************************************************************
     * public Api
     ******************************************************************/

    //游戏重新开始
    public restart(){
        this.findNodeOfPoker(this.node)
       this.pokerArr.forEach((poker)=>{this.pokerNodePool.put(poker)})
        
    }
}
